package com.unifiedlibrary.amall_page.base.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.recyclerview.widget.RecyclerView;

import com.base.common.basemodule.BaseResponsceBean;
import com.base.common.basemodule.CheckNetWorkUtils;
import com.base.common.basemodule.MvpInstantiationUtils;
import com.base.common.widget.ForwardPopWindow;
import com.base.usefulModule.ToastUtil;
import com.unifiedlibrary.amall_page.R;
import com.unifiedlibrary.amall_page.base.model.BaseFragmentModel;
import com.unifiedlibrary.amall_page.base.view.BaseFragment;

import io.reactivex.disposables.Disposable;

import java.util.List;

/**
 * @author xzk
 * @data 2019/2/28
 * @email xiezhengkun@beyondsoft.com
 * @remark fragment的基类
 */
public abstract class BaseFragmentPresenter<M extends BaseFragmentModel, V extends BaseFragment> {
    protected M model;
    protected V context;
    /**
     * 用来跳转界面
     */
    protected Activity activity;
    protected Disposable disposable;

    public void bindViewAndModel(V context) {
        this.context = context;
        activity = (Activity) (context.getSupperContext());
        model = MvpInstantiationUtils.getInstantiation(this, MvpInstantiationUtils.FIRSTGENERICITY);
        if (model != null) {
            model.setPresenter(this);
        }
    }

    /**
     * 回收所有绑定资源
     */
    public void recycleResource(){
        if(model != null){
            model.recycleResource();
            model = null;
        }
        if(context != null){
            context = null;
        }
    }

    protected void showLoadingDialog(){
        if( context == null){
            return;
        }
        context.showLoadingDialog();
    }
    protected void removeLoadingDialog(){
        if( context == null){
            return;
        }
        context.removeLoadingDialog();
    }
    /**
     * 请求异常提示
     */
    protected void showErrorMsg(){
        if( context == null){
            return;
        }
        context.removeLoadingDialog();
        context.closeRefresh();
        ToastUtil.showShort(context.getContext(),R.string.str_system_error);
    }

    /**
     * 提示请求失败的提示语:土司方式
     * @param data
     */
    public void showFailMsg(BaseResponsceBean data){
        if( context == null){
            return;
        }
        context.closeRefresh();
        context.removeLoadingDialog();
        String msg = data.getMessage();
        if(TextUtils.isEmpty(msg)){
            msg = context.getString(R.string.str_system_error);
        }
        ToastUtil.showLong(activity,msg);
    }
    /**
     * 提示请求失败的提示语
     * @param msg
     * @param errHint
     */
    protected void showFailMsg(String msg, int errHint){
        if( context == null){
            return;
        }
        context.closeRefresh();
        removeLoadingDialog();
        if(errHint>0){
            try {
                msg=context.getString(errHint);
            }catch (Exception e){
                msg = context.getString(R.string.str_system_error);
            }
        }else {
            if(TextUtils.isEmpty(msg)){
                msg = context.getString(R.string.str_system_error);
            }
        }
        ToastUtil.showShort(context.getActivity(), msg);
    }
    /**
     * 网络异常提示
     *   没有网，网络不可用
     * @return true 网络可用   false 网络不可用
     */
    protected boolean checkNetIsEnable(){
        if(activity==null){
            return false;
        }
        if(!CheckNetWorkUtils.isNetworkConnected(activity)){
            //网络不可用
            String msg = context.getString(R.string.str_network_not_available);
            ToastUtil.showShort(context.getActivity(), msg);
            return false;
        }
        if(CheckNetWorkUtils.getConnectedType(activity)<0){
            //网络未连接
            String msg = context.getString(R.string.str_network_not_available);
            ToastUtil.showShort(context.getActivity(), msg);
            return false;
        }
        return true;
    }

    /**
     * 弹窗提示,带黑背景的，适应宽高
     * 指定宽度
     */
    protected ForwardPopWindow showWrapPopWindowBack(View tagetView,View contentView,int width){
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(activity).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           ViewGroup.LayoutParams.WRAP_CONTENT)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .enableBackgroundDark(true)
                                                                                       .setBgDarkAlpha(0.3f)
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAsDropDown(tagetView, -40, 0, Gravity.BOTTOM | Gravity.RIGHT);
        return mPopWindow;
    }
    /**
     * 弹窗提示,带黑背景的，适应宽高
     * 指定宽度
     */
    protected ForwardPopWindow showWrapPopWindowBack(View tagetView,View contentView,int width,int height){
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(activity).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                               height)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .enableBackgroundDark(true)
                                                                                       .setBgDarkAlpha(0.3f)
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAsDropDown(tagetView, -40, 0, Gravity.BOTTOM | Gravity.RIGHT);
        return mPopWindow;
    }

    /**
     * 全屏的popwindow
     * @return
     */
    protected ForwardPopWindow showMatchPopWindow(View tagetView,View contentView){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(activity).setView(contentView)
                                                                                       //显示大小
                                                                                       .size(
                                                                                           width,
                                                                                           ViewGroup.LayoutParams.WRAP_CONTENT)
                                                                                       //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                       .setTouchable(true)
                                                                                       .setOutsideTouchable(false)
                                                                                       .create();
        mPopWindow.showAsDropDown(tagetView, 0, 1, Gravity.BOTTOM);
        return mPopWindow;
    }

//    /**
//     * 当选项的数量超过8时，进行高度限制
//     *
//     * @param dataSize
//     * @param recyclerView
//     */
//    protected void limitRecycleViewHeight(int dataSize, RecyclerView recyclerView){
//        if(dataSize > 8){
//            ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
//            int[] screenSize = ScreenUtils.getScreenSize(activity);
//            params.height=screenSize[1]/3;
//            recyclerView.setLayoutParams(params);
//        }
//    }

    /**
     * 全屏的popwindow
     * @return
     */
    protected ForwardPopWindow showMatchParentPopWindow(View tagetView, View contentView){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(activity).setView(contentView)
                                                                                      //显示大小
                                                                                      .size(
                                                                                          width,
                                                                                          ViewGroup.LayoutParams.MATCH_PARENT)
                                                                                      //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                                                                                      .setTouchable(true)
                                                                                      .setOutsideTouchable(false)
                                                                                      .create();
        mPopWindow.showAtLocation(tagetView,  Gravity.BOTTOM, 0, 0);
        return mPopWindow;
    }

    /**
     * 现隐状态栏
     * @param enable
     */
    protected void full(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            activity.getWindow().setAttributes(lp);
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = activity.getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            activity.getWindow().setAttributes(attr);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

}
